package com.dousb.oj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dousb.oj.model.entity.QuestionSubmit;
import com.dousb.oj.common.BaseResponse;
import com.dousb.oj.common.ErrorCode;
import com.dousb.oj.common.ResultUtils;
import com.dousb.oj.exception.BusinessException;
import com.dousb.oj.model.dto.questionsubmit.QuestionSubmitAddRequest;
import com.dousb.oj.model.dto.questionsubmit.QuestionSubmitQueryRequest;
import com.dousb.oj.model.entity.User;
import com.dousb.oj.model.vo.QuestionSubmitVO;
import com.dousb.oj.service.QuestionSubmitService;
import com.dousb.oj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 题目提交接口
 */
@RestController
@RequestMapping("question_submit")
@Slf4j
public class QuestionSubmitController {

    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private UserService userService;

    /**
     * 題目提交接口
     *
     * @param questionSubmitAddRequest
     * @param request
     * @return 提交的记录
     */
    @PostMapping("/")
    public BaseResponse<Long> doQuestionSubmit(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest,
                                                  HttpServletRequest request) {
        // 判断请求参数是否为空或者题目ID是否小于等于0
        if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {
            // 抛出参数错误异常
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 获取登录用户
        final User loginUser = userService.getLoginUser(request);
        // 调用题目提交服务
        long questionSubmitId = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);
        // 返回结果
        return ResultUtils.success(questionSubmitId);
    }

    /**
     * 分页获取题目提交列表（除了管理员外 普通用户只能看见 非答案 提交代码信息
     *
     * @param questionSubmitQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<QuestionSubmitVO>> listQuestionSubmitByPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest
            , HttpServletRequest request) {
        //获取当前页码
        long current = questionSubmitQueryRequest.getCurrent();
        //获取每页显示的记录数
        long size = questionSubmitQueryRequest.getPageSize();
        //从数据库中查询得到的原始的分页的数据 不是数据脱敏过的数据分页
        Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, size),
                questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
        final User loginUser = userService.getLoginUser(request);
        //将原始的分页数据转换为数据脱敏过的数据分页
        return ResultUtils.success(questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage, loginUser));
    }
}
